<%--
 시스템명     : SKT OMP
 업 무 명       : 이미지 다운로드 
 프로그램명 : download.jsp
 설    명         : 이미지를 다운로드 한다.
 작 성 자       : sepiroth
 작 성 일       : 2011. 12. 13.
 수 정 일       : 
 수정내역     : 
--%>
<%@ page language="java" contentType="application/octet-stream; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.omp.admin.stat.tGift.util.CryptoUtil"%>

<%
	String filePath = request.getParameter("key");
	boolean flag = false;
	
	filePath = CryptoUtil.Decode(filePath);
	
	if(filePath.indexOf("./") != -1 ||
	   filePath.indexOf(".\\") != -1 ||
	   filePath.indexOf("..") != -1) {
		flag = false;
	}else {
		flag = true;
	}
	
	if(flag) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {  
			//filePath = ContentConstants.REPOSITORY_PATH_FOR_UPLOAD + filePath;
			//filePath = filePath; // 고화질 동영상은 full path를 사용
			response.setHeader("Content-Type", getServletContext().getMimeType(filePath));
			
			File f = new File(filePath);
			byte b[] = new byte[(int)f.length()];
			response.setContentType("application/unknown; charset=UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(f.getName(), "UTF8") + ";");
			response.setHeader("Content-Type", "application/octet-stream; charset=UTF-8");
			response.setHeader("Content-Length", ""+ f.length());
		//	response.setHeader("Content-Transfer-Encoding", "binary;");
			response.setHeader("Content-Transfer-Encoding" , "base64");
			response.setHeader("Pragma", "no-cache;");
			response.setHeader("Expires", "-1;");
			
			
			if (f.isFile() && f.length() > 0 ) {
			    
				bis = new BufferedInputStream(new FileInputStream(f));
				
				bos = new BufferedOutputStream(response.getOutputStream());
				int read = 0;
				while ((read = bis.read(b)) != -1){
					bos.write(b,0,read);
				}
				
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(bis != null) try { bis.close(); } catch(Exception e) {}
			if(bos != null) try { bos.close(); } catch(Exception e) {}
			out.clear();
			out = pageContext.pushBody();
		}
	}else {
		response.setContentType("application/unknown; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("blank", "UTF8") + ";");
		response.setHeader("Content-Type", "application/octet-stream; charset=UTF-8");
		response.setHeader("Content-Length", "0");
	//	response.setHeader("Content-Transfer-Encoding", "binary;");
		response.setHeader("Content-Transfer-Encoding" , "base64");
		response.setHeader("Pragma", "no-cache;");
		response.setHeader("Expires", "-1;");
	}
%>
